package cn.ctodb.wxmp.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;

import cn.ctodb.wxmp.domain.enumeration.MenuType;

/**
 * A Menu.
 */
@Entity
@Table(name = "menu")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "menu")
public class Menu implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "click_key")
    private String clickKey;

    @Column(name = "lv")
    private Integer lv;

    @Column(name = "name")
    private String name;

    @Column(name = "sort")
    private Integer sort;

    @Enumerated(EnumType.STRING)
    @Column(name = "menu_type")
    private MenuType menuType;

    @Column(name = "url")
    private String url;

    @ManyToOne
    private Menu menu;

    @OneToMany(mappedBy = "menu")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Menu> menus = new HashSet<>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getClickKey() {
        return clickKey;
    }

    public Menu clickKey(String clickKey) {
        this.clickKey = clickKey;
        return this;
    }

    public void setClickKey(String clickKey) {
        this.clickKey = clickKey;
    }

    public Integer getLv() {
        return lv;
    }

    public Menu lv(Integer lv) {
        this.lv = lv;
        return this;
    }

    public void setLv(Integer lv) {
        this.lv = lv;
    }

    public String getName() {
        return name;
    }

    public Menu name(String name) {
        this.name = name;
        return this;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSort() {
        return sort;
    }

    public Menu sort(Integer sort) {
        this.sort = sort;
        return this;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public MenuType getMenuType() {
        return menuType;
    }

    public Menu menuType(MenuType menuType) {
        this.menuType = menuType;
        return this;
    }

    public void setMenuType(MenuType menuType) {
        this.menuType = menuType;
    }

    public String getUrl() {
        return url;
    }

    public Menu url(String url) {
        this.url = url;
        return this;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Menu getMenu() {
        return menu;
    }

    public Menu menu(Menu menu) {
        this.menu = menu;
        return this;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }

    public Set<Menu> getMenus() {
        return menus;
    }

    public Menu menus(Set<Menu> menus) {
        this.menus = menus;
        return this;
    }

    public Menu addMenu(Menu menu) {
        menus.add(menu);
        menu.setMenu(this);
        return this;
    }

    public Menu removeMenu(Menu menu) {
        menus.remove(menu);
        menu.setMenu(null);
        return this;
    }

    public void setMenus(Set<Menu> menus) {
        this.menus = menus;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Menu menu = (Menu) o;
        if (menu.id == null || id == null) {
            return false;
        }
        return Objects.equals(id, menu.id);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public String toString() {
        return "Menu{" +
            "id=" + id +
            ", clickKey='" + clickKey + "'" +
            ", lv='" + lv + "'" +
            ", name='" + name + "'" +
            ", sort='" + sort + "'" +
            ", menuType='" + menuType + "'" +
            ", url='" + url + "'" +
            '}';
    }
}
